﻿Shader "Dan/UI_FlashingText"
{
    Properties
    {
       
        _MainTex ("MainTex", 2D) = "white" {}
        [HDR]_Color ("Color", Color) = (1,0.75,0.1,1)
        _ColorBG ("ColorBG", Color) = (0,0.1,0.1,1)
        _TextGlow ("TextGlow", Float ) = 1
        _FlashContrast ("FlashContrast", Range(0, 1) ) = 0.1
        _FlashFrequency ("FlashFrequency", Float ) = 1
        _MoveSpeed ("MoveSpeed", Float ) = 0.5
        _MoveStep ("MoveStep", Float ) = 4
        [MaterialToggle] _UseSpotGrid ("UseSpotGrid", Float ) = 0
        [MaterialToggle] _UseLineGrid ("UseLineGrid", Float ) = 0
        _GridAmount ("Grid Amount", Float ) = 48 
        _GridSize ("GridSize", Range(0, 1)) = 0.5
        [MaterialToggle] _UseAlpha ("UseAlpha", Float ) = 1
    }
    SubShader
    {
        Tags { "Queue"="AlphaTest" "RenderType"="Transparent" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float4 _Color;
            float _FlashContrast;
            float4 _ColorBG;
            float _TextGlow;
            
            float _FlashFrequency;
            fixed _UseAlpha;
            float _MoveSpeed;
            float _MoveStep;
            float _GridAmount;
            float _GridSize;
            fixed _UseSpotGrid;
            fixed _UseLineGrid;
            
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                
                return o;
            }
            float UVMaskLine (float uvdir, float amount, float width){float d = abs((frac(uvdir*amount)-0.5)*2);return floor(d+width);}

            float UVMaskSpots(float2 uv, float amount, float size){ uv= (frac(uv*amount)-0.5)*2; return floor(1-length(uv)+size);}
            fixed4 frag (v2f i) : SV_Target
            {
                
                float2 uv = i.uv;
                //uv间歇闪烁
                float offsetTime =fmod(_Time.y*_MoveSpeed,_MoveStep);
                float flowX = floor(offsetTime)/_MoveStep;
               //移动UV
                float2 moveUV = uv+float2(flowX,0);
                //采样贴图
                moveUV =TRANSFORM_TEX(moveUV, _MainTex);
                float4 mainColor = tex2D(_MainTex,moveUV);
                float alpha = ceil(mainColor.a);
                    
                //画网格
                float lineGrid = UVMaskLine(uv.x , _GridAmount ,_GridSize*1.2);
                float spotGrid = UVMaskSpots(uv,_GridAmount ,_GridSize*1.2);
                //是否开启网格
                float useSpotGrid = lerp( 1 , saturate(spotGrid), _UseSpotGrid);
                float useLineGrid = lerp( 1 , saturate(lineGrid), _UseLineGrid);
                //是否启用透明
                float useAlpha =lerp( 1.0, alpha*useSpotGrid*useLineGrid, _UseAlpha );
                //裁剪
                clip(useAlpha - 0.5);
                //闪烁速度
                float flashSpeed =frac(_Time.y*_FlashFrequency)+_FlashContrast;

                
                //发光值
                float emissionVal = alpha*flashSpeed;
                 
                //颜色
                float3 finalColor = lerp(_ColorBG.rgb,_Color.rgb*_TextGlow,emissionVal) * useSpotGrid*useLineGrid;
                
                
                return fixed4(finalColor,1);
                
                
            }
            ENDCG
        }
    }
}
